<template>
  <div>
    <div class="handle-box">
      <el-button type="primary" class="handle-del mr10" @click="addClick">新增产品类别</el-button>
      <el-date-picker class="mr10" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd"
                      v-model="query.time">
      </el-date-picker>
      <el-input placeholder="请输入产品名称查询" v-model="query.souVal" class="handle-input mr10"></el-input>
      <el-button type="primary" icon="el-icon-search" @click="souClick">搜索</el-button>
    </div>
    <el-table :data="tableData" v-loading="listLoad" border class="table" style="width: 100%" ref="multipleTable"
              header-cell-class-name="table-header" @selection-change="SelectionChange">
      <el-table-column type="selection" width="55" align="center"></el-table-column>
      <el-table-column type="index" :index="tableIndex" label="序号" width="55" align="center"></el-table-column>
      <el-table-column prop="relation" label="选项"></el-table-column>
      <el-table-column fixed="right" label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="text" icon="el-icon-edit" class="red" @click="editinit(scope.row)">编辑</el-button>
          <el-button type="text" icon="el-icon-delete" class="red" @click="delClick(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination background layout="total, prev, pager, next" :current-page="query.page" :page-size="query.limit" :total="page.totalCount"
                     :pager-count="5" @current-change="PageChange" @prev-click="PageChange" @next-click="PageChange"></el-pagination>
    </div>
    <!-- 新增产品类别 -->
    <el-dialog title="新增产品类别" :visible.sync="addShopDialog.addShopDialogFlg" width="30%">
      <el-form ref="form" :model="addShopDialog.form" label-width="80px">
        <el-form-item label="选项">
          <el-cascader v-if="editId == ''" v-model="addShopDialog.form.selectedVal" :options="addShopDialog.options" clearable style="width: 100% ">
          </el-cascader>
          <el-input v-else v-model="editName" disabled></el-input>
        </el-form-item>
        <el-form-item label="子级选项" v-for="item in addShopDialog.form.er_arr">
          <el-input v-model="item.name"></el-input>
        </el-form-item>
        <el-form-item label="">
          <el-button type="text" @click="addErArrPush" v-if="editId == ''">+ 添加</el-button>
        </el-form-item>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="addShopDialog.addShopDialogFlg = false">取 消</el-button>
        <el-button type="primary" @click="addSub" :loading="addShopDialog.load">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { classManageShopClassSel, classManageAddSub, classManageGetTable, classManageEditSub, classManageDelSub } from '../../../../api/index';
export default {
  data() {
    return {
      listLoad: false,
      query: {
        page: 1,
        limit: 10,
        souVal: '',
        time: [],
      },
      tableLoad: false,
      tableData: [],
      page: {
        totalCount: 10
      },
      addShopDialog: {
        addShopDialogFlg: false,
        load: false,
        form: {
          selectedVal: [],
          er_arr: []
        },
        options: []
      },
      editId: '',
      editprevId: '',
      editName: '',
    };
  },
  created() {
    this.getTable();
    this.getClassData();
  },
  methods: {
    //获取列表
    getTable() {
      this.tableLoad = true;
      let data = {
        page: this.query.page,
        startTime: this.query.time[0] ? this.query.time[0] : '',
        endTime: this.query.time[1] ? this.query.time[1] : '',
        limit: this.query.limit,
        kw: this.query.souVal
      }
      classManageGetTable(data).then((res) => {
        this.tableLoad = false;
        if (res.code == 0) {
          this.tableData = res.recordList;
          this.page.totalCount = res.totalCount;
        } else {
          this.$message(res.msg);
        }
      })
    },
    //获取分类下拉
    getClassData() {
      this.addShopDialog.options = [];
      classManageShopClassSel().then((res) => {
        if (res.code == 0) {
          let that = this;
          res.list.forEach((item, i) => {
            that.addShopDialog.options.push({
              value: item.id,
              label: item.title,
              children: item.child.length > 0 ? [] : ''
            });
            if (item.child.length > 0) {
              item.child.forEach((e, index) => {
                that.addShopDialog.options[i].children.push({
                  value: e.id,
                  label: e.title,
                })
              })
            }
          })
        } else {
          this.$message(res.msg);
        }
      })
    },
    //多个添加
    addErArrPush() {
      this.addShopDialog.form.er_arr.push({ name: '' });
    },
    //提交
    addSub() {
      this.addShopDialog.load = true;

      if (this.editId) {
        let data = {
          id: this.editId,
          title: this.addShopDialog.form.er_arr[0].name,
          parentId: this.editprevId
        }
        classManageEditSub(data).then((res) => {
          this.addShopDialog.load = false;
          if (res.code == 0) {
            this.$message.success("修改成功");
            this.editId = "";
            this.addShopDialog.addShopDialogFlg = false;
            this.getTable();
          } else {
            this.$message(res.msg);
          }
        })
      } else {
        let data = {
          childName: (() => {
            let arr = [];
            this.addShopDialog.form.er_arr.forEach((item, i) => {
              arr.push(item.name);
            })
            return arr;
          })(),
          parentId: this.addShopDialog.form.selectedVal[this.addShopDialog.form.selectedVal.length - 1],
        }
        classManageAddSub(data).then((res) => {
          this.addShopDialog.load = false;
          if (res.code == 0) {
            this.$message.success("新增成功");
            this.addShopDialog.addShopDialogFlg = false;
            this.getTable();
          } else {
            this.$message(res.msg);
          }
        })
      }
    },
    //序号
    tableIndex(index) {
      return (this.query.page - 1) * this.query.limit + index + 1;
    },
    //翻页
    PageChange(num) {
      this.query.page = num;
      this.getTable();
    },
    //搜索
    souClick() {
      this.query.page = 1;
      this.getTable();
    },
    //选中
    SelectionChange() { },
    //新增
    addClick() {
      this.addShopDialog.addShopDialogFlg = true;
      this.addShopDialog.form.er_arr = [];
      this.addShopDialog.form.selectedVal = [];
      this.getClassData();
    },
    //编辑
    editinit(item) {
      console.log(item)
      this.editId = item.id;
      this.editprevId = item.parentId;
      this.editName = item.relation;
      this.addShopDialog.form.er_arr = [];
      this.addShopDialog.form.er_arr.push({ name: item.title });
      this.addShopDialog.addShopDialogFlg = true;
    },
    //删除
    delClick(id) {
      this.listLoad = true;
      classManageDelSub({ id: id }).then((res) => {
        this.listLoad = false;
        if (res.code == 0) {
          this.$message.success("删除成功");
          this.getTable();
        } else {
          this.$message(res.msg);
        }
      })
    }
  }
};
</script>

<style scoped>
.handle-input {
    width: 300px;
    display: inline-block;
}
</style>